﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScormModel;

namespace WpfELearningAuthoringTool
{
    class ManifestProcessing
    {
        ImsManifest imsmanifest;
        SwapInfo swapInfo = new SwapInfo();
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public ManifestProcessing(ImsManifest imsmanifest)
        {
            this.imsmanifest = imsmanifest;
        }
        public Organization findOrganizationByTitle(string title)
        {
            for (int i = 0; i < imsmanifest.ManifestOrganizations.ListOrganization.Count; i++)
            {
                Organization org = imsmanifest.ManifestOrganizations.ListOrganization[i];
                if (org.Title == title)
                    return org;
                else
                    continue;
            }
            return null;
        }
        public Item findItem(string title)
        {
            for (int i = 0; i < imsmanifest.ManifestOrganizations.ListOrganization.Count; i++)
            {
                Organization org = imsmanifest.ManifestOrganizations.ListOrganization[i];
                Item item = findItemByTitle(org.ListItem, title);
                if (item != null)
                    return item;
            }
            return null;
        }
        public Item findItemByTitle(List<Item> listItem, string title)
        {

            List<Item> temp = new List<Item>();
            for (int j = 0; j < listItem.Count; j++)
            {
                temp.Add(listItem[j]);
            }
            for (int i = 0; i < temp.Count; i++)
            {
                Item item = temp[i];
                if (item.Title == title)
                    return item;
                else if (item.ListChildItem.Count > 0)
                    for (int j = 0; j < item.ListChildItem.Count; j++)
                    {
                        temp.Add(item.ListChildItem[j]);
                    }
                else
                    continue;
            }
            return null;
        }
        public List<Item> findReourceItems()
        {
            List<Item> resourceItemList = new List<Item>();
            List<Item> temp = new List<Item>();
            for (int j = 0; j < imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem.Count; j++)
            {
                temp.Add(imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[j]);
            }
            for (int i = 0; i < temp.Count; i++)
            {
                Item item = temp[i];
                if (item.Identifierref != null && item.Identifierref.Length > 0)
                {
                    resourceItemList.Add(item);
                }
                if (item.ListChildItem.Count > 0)
                {
                    for (int j = 0; j < item.ListChildItem.Count; j++)
                    {
                        temp.Add(item.ListChildItem[j]);
                    }
                }

            }
            return resourceItemList;
        }
        public void BuildTitleHistory()
        {
            imsmanifest.ListTitleHistory = new System.Collections.Hashtable();
            List<Item> temp = new List<Item>();
            for (int j = 0; j < imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem.Count; j++)
            {
                temp.Add(imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[j]);
            }

            for (int i = 0; i < temp.Count; i++)
            {
                Item item = temp[i];
                if (!imsmanifest.ListTitleHistory.ContainsKey(item.Title))
                    imsmanifest.ListTitleHistory.Add(item.Title, item.Title);
            }
        }
        public Organization findParentOfModule(Item seletedItem)
        {
            for (int i = 0; i < imsmanifest.ManifestOrganizations.ListOrganization.Count; i++)
            {
                Organization org = imsmanifest.ManifestOrganizations.ListOrganization[i];
                for (int j = 0; j < org.ListItem.Count; j++)
                {
                    if (seletedItem.Title == org.ListItem[j].Title)
                    {
                        swapInfo.CurItemIndex = j;
                        if (j + 1 < org.ListItem.Count)
                        {
                            swapInfo.NextItemIndex = j + 1;
                        }
                        else {
                            swapInfo.NextItemIndex = -1;
                        }
                        if (j - 1 >= 0)
                        {
                            swapInfo.PreItemIndex = j - 1;
                        }
                        else {
                            swapInfo.PreItemIndex = -1;
                        }
                        return org;
                    }
                }

            }
            return null;
        }
        public Item findParentOfItem(Item seletedItem)
        {
            for (int i = 0; i < imsmanifest.ManifestOrganizations.ListOrganization.Count; i++)
            {
                Organization org = imsmanifest.ManifestOrganizations.ListOrganization[i];
                Item parentItem = findInParents(org.ListItem, seletedItem);
                if (parentItem != null)
                    return parentItem;
            }
            return null;
        }
        public Item findInParents(List<Item> listItem, Item seletedItem)
        {
            List<Item> temp = new List<Item>();
            for (int j = 0; j < listItem.Count; j++)
            {
                temp.Add(listItem[j]);
            }
            for (int i = 0; i < temp.Count; i++)
            {
                Item item = temp[i];
                Item parent = findParent(item, seletedItem);
                if (parent != null)
                {
                    for (int k = 0; k < parent.ListChildItem.Count; k++)
                    {
                        if (parent.ListChildItem[k].Title == seletedItem.Title)
                        {
                            swapInfo.CurItemIndex = k;
                            if (k + 1 < parent.ListChildItem.Count)
                            {
                                swapInfo.NextItemIndex = k + 1;
                            }
                            else
                            {
                                swapInfo.NextItemIndex = -1;
                            }
                            if (k - 1 >= 0)
                            {
                                swapInfo.PreItemIndex = k - 1;
                            }
                            else
                            {
                                swapInfo.PreItemIndex = -1;
                            }
                        }
                        else
                            continue;
                    }
                    return parent;
                }
                else if (item.ListChildItem.Count > 0)
                    for (int j = 0; j < item.ListChildItem.Count; j++)
                    {
                        temp.Add(item.ListChildItem[j]);
                    }
                else
                    continue;
            }
            return null;
        }
        public Item findParent(Item parent, Item seletedItem)
        {
            for (int i = 0; i < parent.ListChildItem.Count; i++)
            {
                Item itemi = parent.ListChildItem[i];
                if (itemi.Title == seletedItem.Title)
                    return parent;
                else
                    continue;
            }
            return null;
        }
        public string findTypeOfItem(string title)
        {
            string type = "";
            Organization org = findOrganizationByTitle(title);
            if (org != null)
                type = org.Type;
            Item item = findItem(title);
            if (item != null)
                type = item.ItemType;
            return type;
        }
        public Organization findOrganizationByName(string title)
        {
            Organization org = findOrganizationByTitle(title);
            return org;
        }
        public Item findItemByName(string title)
        {
            Item item = findItem(title);
            return item;
        }
        public string getNameOfTreeViewItem(TreeViewItem selectedItem)
        {
            StackPanel sp = null;
            Label setectItemLabel = null;
            sp = (StackPanel)selectedItem.Header;
            foreach (var child in sp.Children)
            {
                if (child.GetType().ToString() == "System.Windows.Controls.Label")
                    setectItemLabel = (Label)child;

            }
            if (setectItemLabel != null)
                return (setectItemLabel.Content).ToString();
            else
                return "Undefine";
        }
        public Boolean addItem(Item parent, Item child, string template)
        {
            Boolean result = false;
            switch (template)
            {
                case "None":
                    result = addItemForNoneTemplate(parent, child);
                    break;
                case "Linear":
                    break;
                case "Linear_Choice":
                    break;
                case "Linear_Controls":
                    break;
                case "Constrained_Choice":
                    break;
                case "Competency":
                    break;
                case "KnowledgePaced":
                    break;
                case "KnowledgePaced_Reuse":
                    break;
                case "Remediation":
                    break;
                default:
                    break;
            }
            return result;
        }
        public int getNumOfResources()
        {
            return imsmanifest.Resources.ListResource.Count;
        }
        public Resource findResourceForItem(Item item)
        {
            if (imsmanifest.Resources == null)
                return null;
            for (int i = 0; i < imsmanifest.Resources.ListResource.Count; i++)
            {
                if (imsmanifest.Resources.ListResource[i].Identifier == item.Identifierref)
                    return imsmanifest.Resources.ListResource[i];
                else
                    continue;
            }
            return null;
        }
        public void handleDeleteResource(Item item)
        {
            if (imsmanifest.Resources == null)
                return ;
            if (item.Identifierref == null)
                return;
            else
            {
                for (int i = 0; i < imsmanifest.Resources.ListResource.Count; i++)
                {
                    if (imsmanifest.Resources.ListResource[i].Identifier == item.Identifierref)
                    {
                        imsmanifest.Resources.ListResource.RemoveAt(i);
                        return;
                    }
                    else
                        continue;
                }
            }
        }
        public void addresource(Resource re)
        {
            imsmanifest.Resources.ListResource.Add(re);
        }

        public void BuildModuleObjecttiveHashTable()
        {
            List<Item> temp = new List<Item>();
            for (int j = 0; j < imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem.Count; j++)
            {
                temp.Add(imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[j]);
            }
            for (int i = 0; i < temp.Count; i++)
            {
                Item item = temp[i];
                if (item.ItemType == "MODULE")
                {
                    if (item.Sequencing.Objectives.PrimaryObjective.ListMapInfo != null && item.Sequencing.Objectives.PrimaryObjective.ListMapInfo.Count > 0)
                    {
                        if (item.Sequencing.Objectives.PrimaryObjective.ListMapInfo[0].TargetObjectiveID != null)
                        {
                            string objid = item.Sequencing.Objectives.PrimaryObjective.ListMapInfo[0].TargetObjectiveID;
                            if (!imsmanifest.ListModuleObjective.ContainsKey(item.Title))
                            {
                                imsmanifest.ListModuleObjective.Add(item.Title, objid);
                                LinkModuleObjective llo = new LinkModuleObjective();
                                llo.ModuleName = item.Title;
                                llo.ObjectiveId = objid;
                                imsmanifest.ListObjectiveLog.Add(llo);
                            }
                        }
                    }
                }
                if (item.ListChildItem.Count > 0)
                {
                    for (int j = 0; j < item.ListChildItem.Count; j++)
                    {
                        temp.Add(item.ListChildItem[j]);
                    }
                }

            }
        }
        #region None Template
        /*-------------------------Begin None template----------------------------------*/
        public Boolean addItemForNoneTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                if (parent.ItemType == "MODULE")
                {
                    parent.ListChildItem.Add(child);
                }
                else if (parent.ItemType == "EXAM")
                {
                    parent.ListChildItem.Add(child);
                }
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addModuleForNoneTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                Item examItem = parent.ListItem[parent.ListItem.Count - 1];
                parent.ListItem.RemoveAt(parent.ListItem.Count - 1);
                parent.ListItem.Add(child);
                parent.ListItem.Add(examItem);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForNoneTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item"+DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initQuestionItemForNoneTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForNoneTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForNoneTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForNoneTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForNoneTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                if (item.ItemType == "MODULE")
                {
                    Organization org = findParentOfModule(item);
                    org.ListItem.Remove(item);
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    parent.ListChildItem.Remove(item);
                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        /*------------------------End None Template----------------------------*/
        #endregion
        #region Custom Template
        public Boolean addItemForCustomTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addItemForCustomTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForCustomTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = new Sequencing();
            newItem.Sequencing.Objectives = new Objectives();
            newItem.Sequencing.Objectives = SequenceProcessing.buildObjectiveForModuleForCustomTemplate(DateTime.Now.Ticks.ToString());
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initQuestionItemForCustomTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForCustomTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initExamItemForCustomTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "EXAM";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initTestItemForCustomTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "TEST";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForCustomTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Organization org = findParentOfModule(item);
                if (org != null)
                {
                    org.ListItem.Remove(item);
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    if(parent != null)
                        parent.ListChildItem.Remove(item);
                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        #endregion
        #region Linear Template
        /*------------------------Begin Linenear template-------------------------------*/
        public Boolean addItemForLinearTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                if (parent.ItemType == "MODULE")
                {
                    parent.ListChildItem.Add(child);
                }
                else if (parent.ItemType == "EXAM")
                {
                    parent.ListChildItem.Add(child);
                }
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addModuleForLinearTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                Item examItem = parent.ListItem[parent.ListItem.Count - 1];
                parent.ListItem.RemoveAt(parent.ListItem.Count - 1);
                parent.ListItem.Add(child);
                parent.ListItem.Add(examItem);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForLinearTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initQuestionItemForLinearTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForLinearTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
          //  newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForLinearTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForLinearTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForLinearTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                if (item.ItemType == "MODULE")
                {
                    Organization org = findParentOfModule(item);
                    org.ListItem.Remove(item);
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    parent.ListChildItem.Remove(item);
                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        /*------------------------End Linenear template----------------------------------*/
        #endregion
        #region Linear Choice Template
        /*------------------------Begin Linear Choice Template---------------------------*/
        public Boolean addItemForLinearChoiceTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                if (parent.ItemType == "MODULE")
                {
                    Item examItem = parent.ListChildItem[parent.ListChildItem.Count - 1];
                    parent.ListChildItem.RemoveAt(parent.ListChildItem.Count - 1);
                    parent.ListChildItem.Add(child);
                    parent.ListChildItem.Add(examItem);
                    result = true;
                }
                else
                {
                    parent.ListChildItem.Add(child);
                    result = true;
                }
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForLinearChoiceTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = sp.buildModuleSequencingForLinearChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            Item examItem = new Item();
            examItem.Title = "Exam For " + input.Text;
            examItem.ItemType = "EXAM";
            examItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            examItem.ListChildItem = new List<Item>();
            examItem.Sequencing = sp.buildExamSequencingForLinearChoiceTemplate();
            newItem.ListChildItem.Add(examItem);
            return newItem;
        }
        public Item initLessonItemForLinearChoiceTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.Sequencing = sp.buildLessonSequencingForLinearChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForLinearChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForLinearChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForLinearChoiceTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Item parent = findParentOfItem(item);
                parent.ListChildItem.Remove(item);
                result = true;
                handleDeleteResource(item);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initQuestionItemForLinearChoiceTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        /*------------------------End Linear Choice Template---------------------------*/
        #endregion
        #region Constrained Choice Template
        /*------------------------Begin Constrained Choice Template----------------------*/
        public Boolean addItemForConstrainedChoiceTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                if (parent.ItemType == "MODULE")
                {
                    Item examItem = parent.ListChildItem[parent.ListChildItem.Count - 1];
                    parent.ListChildItem.RemoveAt(parent.ListChildItem.Count - 1);
                    parent.ListChildItem.Add(child);
                    parent.ListChildItem.Add(examItem);
                    result = true;
                }
                else
                {
                    parent.ListChildItem.Add(child);
                    result = true;
                }
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addModuleForConstrainedTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                Item examItem = parent.ListItem[parent.ListItem.Count - 1];
                parent.ListItem.RemoveAt(parent.ListItem.Count - 1);
                parent.ListItem.Add(child);
                parent.ListItem.Add(examItem);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForConstrainedChoiceTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = sp.buildModuleSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            Item examItem = new Item();
            examItem.Title = "Exam For " + input.Text;
            examItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            examItem.ItemType = "EXAM";
            examItem.ListChildItem = new List<Item>();
            examItem.Sequencing = sp.buildExamSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem.Add(examItem);
            return newItem;
        }
        public Item initLessonItemForConstrainedChoiceTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.Sequencing = sp.buildLessonSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForConstrainedChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForConstrainedChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForConstrainedChoiceTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Item parent = findParentOfItem(item);
                parent.ListChildItem.Remove(item);
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initQuestionItemForConstrainedChoiceTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        /*------------------------End Constrained Choice Template----------------------*/
        #endregion
        #region Linear Control Template
        /*------------------------Begin Linear Control Template-------------------------*/
        public Boolean addItemForLinearControlTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addModuleForLinearControlTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                Item examItem = parent.ListItem[parent.ListItem.Count - 1];
                parent.ListItem.RemoveAt(parent.ListItem.Count - 1);
                parent.ListItem.Add(child);
                parent.ListItem.Add(examItem);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameNodeForLinearControlTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForLinearControlTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForLinearControlTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                if (item.ItemType == "MODULE")
                {
                    Organization org = findParentOfModule(item);
                    org.ListItem.Remove(item);
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    parent.ListChildItem.Remove(item);
                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForLinearControlTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = sp.buildModuleSequencingForLinearControlTemplate();
            newItem.Presentation = sp.buildModulePresentationForLinearControlTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForLinearControlTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.Sequencing = sp.buildLessonSequencingForLinearChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initQuestionItemForLinearControlTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.Presentation = sp.buildQuestionPresentationForLinearControlTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        /*------------------------End Linear Control Template --------------------------*/
        #endregion
        #region Competency Template
        /*------------------------Begin Competency Template-------------------------*/
        public Boolean addItemForCompetencyTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addModuleCompetencyTemplate(Organization parent, Item child)
        {
            Boolean result = false;
            try
            {
                string tagetObj = DateTime.Now.Ticks.ToString();
                child.Sequencing = SequenceProcessing.buildModuleSequencingForCompetencyTemplate(tagetObj);
                Item examItem = parent.ListItem[parent.ListItem.Count - 1];
                parent.ListItem.RemoveAt(parent.ListItem.Count - 1);
                parent.ListItem.Add(child);
                parent.ListItem.Add(examItem);
                Item examPartItem = new Item();
                examPartItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
                examPartItem.Isvisible = "false";
                examPartItem.Title = "Exam For " + child.Title;
                examPartItem.ItemType = "PART";
                examPartItem.ListChildItem = new List<Item>();
                examItem.Sequencing = SequenceProcessing.buildExamPartSequencingForCompetencyTemplate(tagetObj);
                parent.ListItem[parent.ListItem.Count - 1].ListChildItem.Add(examPartItem);
                //update list obj of pretest
                updateSequencingInfo(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForCompetencyTemplate(TextBox input, Organization parent)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            //  newItem.Sequencing = sp.buildModuleSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForCompetencyTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            //  newItem.Sequencing = sp.buildLessonSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForCompetencyTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForCompetencyTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForCompetencyTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                if (item.ItemType != "MODULE")
                {
                    Item parent = findParentOfItem(item);
                    parent.ListChildItem.Remove(item);
                }
                else
                {
                    int index = 0;
                    Organization parent = findParentOfModule(item);
                    //get position of removed module
                    for (int i = 0; i < parent.ListItem.Count; i++)
                    {
                        if (item == parent.ListItem[i])
                        {
                            index = i;
                            break;
                        }
                        else
                            continue;
                    }
                    int realIndex = index - 2;
                    if (realIndex > 0)
                    {
                        int size = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem.Count;
                        imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[size - 1].ListChildItem.RemoveAt(realIndex);
                    }
                    //update list obj of pretest
                    deleteSequencingInfo(item);

                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initQuestionItemForCompetencyTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        private void updateSequencingInfo(Item item)
        {
            string id = item.Sequencing.Objectives.PrimaryObjective.ListMapInfo[0].TargetObjectiveID;
            Item pretest = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[1];
            List<Objective> listObj = pretest.Sequencing.Objectives.ListObjective;
            Objective obj = new Objective();
            obj.ObjectiveID = id;
            obj.ListMapInfo = new List<MapInfo>();
            MapInfo mi = new MapInfo();
            mi.TargetObjectiveID = id;
            mi.ReadNormalizedMeasure = "false";
            mi.ReadSatisfiedStatus = "false";
            mi.WriteSatisfiedStatus = "true";
            obj.ListMapInfo.Add(mi);
            listObj.Add(obj);
        }
        public List<Objective> getCompetencyObjecttives()
        {
            Item pretest = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[1];
            List<Objective> listObj = pretest.Sequencing.Objectives.ListObjective;
            return listObj;
        }
        private void deleteSequencingInfo(Item item)
        {
            string id = item.Sequencing.Objectives.PrimaryObjective.ListMapInfo[0].TargetObjectiveID;
            Item pretest = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[1];
            List<Objective> listObj = pretest.Sequencing.Objectives.ListObjective;
            for (int i = 0; i < listObj.Count; i++)
            {
                if (listObj[i].ObjectiveID == id)
                {
                    listObj.Remove(listObj[i]);
                    break;
                }
                else
                    continue;
            }
        }
        /*------------------------End Competency Template --------------------------*/
        #endregion
        #region Knowledge Paced Template
        /*------------------------Begin KnowledgePaced Template-------------------------*/
        public Boolean addItemForKnowledgePacedTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForKnowledgePacedTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            string targetObj = DateTime.Now.Ticks.ToString();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = sp.buildModuleSequencingForKnowledgePacedTemplate();
            newItem.ListChildItem = new List<Item>();
            Item pretestItem = new Item();
            pretestItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            pretestItem.Title = input.Text + " PreTest";
            pretestItem.ItemType = "PRETEST";
            pretestItem.ListChildItem = new List<Item>();
            pretestItem.Sequencing = sp.buildPretestSequencingForKnowledgePacedTemplate(targetObj);
            Item lessonsItem = new Item();
            lessonsItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            lessonsItem.Title = input.Text + " Lessons";
            lessonsItem.ItemType = "LESSONS";
            lessonsItem.ListChildItem = new List<Item>();
            lessonsItem.Sequencing = sp.buildLessonSequencingForKnowledgePacedTemplate(targetObj);
            Item posttestItem = new Item();
            posttestItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            posttestItem.Title = input.Text + " PostTest";
            posttestItem.ItemType = "POSTTEST";
            posttestItem.ListChildItem = new List<Item>();
            posttestItem.Sequencing = sp.buildPosttestSequencingForKnowledgePacedTemplate(targetObj);

            newItem.ListChildItem.Add(pretestItem);
            newItem.ListChildItem.Add(lessonsItem);
            newItem.ListChildItem.Add(posttestItem);
            return newItem;
        }
        public Item initLessonItemForKnowledgePacedTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            //  newItem.Sequencing = sp.buildLessonSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForKnowledgePacedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForKnowledgePacedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForKnowledgePacedTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Item parent = findParentOfItem(item);
                parent.ListChildItem.Remove(item);
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initQuestionItemForKnowledgePacedTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public List<string> getListObjective()
        {
            List<string> listObj = new List<string>();
            for (int i = 0; i < imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[1].ListChildItem.Count; i++)
            {
                Item item = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem[1].ListChildItem[i];
                if (item.ItemType == "MODULE")
                {
                    string obj = item.ListChildItem[0].Sequencing.Objectives.PrimaryObjective.ListMapInfo[0].TargetObjectiveID;
                    listObj.Add(obj);
                }
                else {
                    continue;
                }
            }
            return listObj;
        }
        /*------------------------End KnowledgePaced Template --------------------------*/
        #endregion
        #region Knowledge Paced Reused Template
        public Boolean addItemForKnowledgePacedReusedTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForKnowledgePacedReusedTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            string targetObj = DateTime.Now.Ticks.ToString();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "MODULE";
            newItem.Sequencing = sp.buildModuleSequencingForKnowledgePacedReusedTemplate();
            newItem.ListChildItem = new List<Item>();
            Item pretestItem = new Item();
            pretestItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            pretestItem.Title = input.Text + " PreTest";
            pretestItem.ItemType = "PRETEST";
            pretestItem.ListChildItem = new List<Item>();
            pretestItem.Sequencing = sp.buildPretestSequencingForKnowledgePacedReusedTemplate(targetObj);
            Item lessonsItem = new Item();
            lessonsItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            lessonsItem.Title = input.Text + " Lessons";
            lessonsItem.ItemType = "LESSONS";
            lessonsItem.ListChildItem = new List<Item>();
            lessonsItem.Sequencing = sp.buildLessonSequencingForKnowledgePacedReusedTemplate(targetObj);
            Item posttestItem = new Item();
            posttestItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            posttestItem.Title = input.Text + " PostTest";
            posttestItem.ItemType = "POSTTEST";
            posttestItem.ListChildItem = new List<Item>();
            posttestItem.Sequencing = sp.buildPosttestSequencingForKnowledgePacedReusedTemplate(targetObj);

            newItem.ListChildItem.Add(pretestItem);
            newItem.ListChildItem.Add(lessonsItem);
            newItem.ListChildItem.Add(posttestItem);
            return newItem;
        }
        public Item initLessonItemForKnowledgePacedReusedTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            //  newItem.Sequencing = sp.buildLessonSequencingForConstrainedChoiceTemplate();
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForKnowledgePacedReusedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForKnowledgePacedReusedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForKnowledgePacedReusedTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Item parent = findParentOfItem(item);
                parent.ListChildItem.Remove(item);
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initQuestionItemForKnowledgePacedReusedTemplate(TextBox input)
        {
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        #endregion
        #region Remediation Template
        private Item getFirstExamItem()
        {
            List<Item> listItem = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem;
            for (int i = 1; i < listItem.Count; i++)
            {
                if (listItem[i].ItemType == "FIRST_EXAM")
                {
                    return listItem[i];
                }
                else {
                    continue;
                }
            }
            return null;
        }
        private int getLastestModule()
        {
            List<Item> listItem = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem;
            for (int i = 0; i < listItem.Count; i++)
            {
                if (listItem[i].ItemType == "FIRST_EXAM")
                {
                    return i - 1;
                }
                else
                {
                    continue;
                }
            }
            return 0;
        }
        private Item getExamItem()
        {
            List<Item> listItem = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem;
            return listItem[listItem.Count - 1];
        }
        public Boolean addModuleForRemediationTemplate(Organization parent, Item child)
        {
            bool result = false;
            try
            {
                child.Sequencing = SequenceProcessing.buildModuleSequencingForRemediationTemplate();
                List<Item> listItem = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem;
                int latestModuleIndex = getLastestModule();

                //add new module
                listItem.Insert(latestModuleIndex + 1, child);
                string targetId = DateTime.Now.Ticks.ToString();
                //add first exam part
                Item fistExamItem = getFirstExamItem();
                Item firstExamPartItem = new Item();
                firstExamPartItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
                firstExamPartItem.ItemType = "PART";
                firstExamPartItem.Identifier = "fp" + child.Identifier;
                firstExamPartItem.Title = "First Exam For " + child.Title;
                firstExamPartItem.ListChildItem = new List<Item>();
                firstExamPartItem.Sequencing = SequenceProcessing.buildFirstExamPartSequencingForRemediationTemplate(targetId);
                fistExamItem.ListChildItem.Add(firstExamPartItem);

                //add Remediate module
                Item remediateItem = new Item();
                remediateItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
                remediateItem.ItemType = "REMEDIATE_MODULE";
                remediateItem.Identifier = "rm" + child.Identifier;
                remediateItem.Title = "Remediate module for " + child.Title;
                remediateItem.ListChildItem = new List<Item>();
                remediateItem.Sequencing = SequenceProcessing.buildRemediateModuleSequencingForRemediationTemplate(targetId);
                listItem.Insert(listItem.Count - 1, remediateItem);

                //add final exam part
                Item finalExamPartItem = new Item();
                finalExamPartItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
                finalExamPartItem.ItemType = "PART";
                finalExamPartItem.Identifier = "fe" + child.Identifier;
                finalExamPartItem.Title = "Final Exam For " + child.Title;
                finalExamPartItem.Sequencing = SequenceProcessing.buildFinalExamPartSequencingForRemediationTemplate(targetId);
                finalExamPartItem.ListChildItem = new List<Item>();

                Item finalExamItem = getExamItem();
                finalExamItem.ListChildItem.Add(finalExamPartItem);

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addItemForRemediationTemplate(Item parent, Item child)
        {
            Boolean result = false;
            try
            {
                parent.ListChildItem.Add(child);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Item initModuleItemForRemediationTemplate(TextBox input, Organization parent)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.Identifier = DateTime.Now.Ticks.ToString();
            newItem.ItemType = "MODULE";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initLessonItemForRemediationTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "LESSON";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Item initQuestionItemForRemediationTemplate(TextBox input)
        {
            SequenceProcessing sp = new SequenceProcessing();
            Item newItem = new Item();
            newItem.Identifier = "item" + DateTime.Now.Ticks.ToString();
            newItem.Title = input.Text;
            newItem.ItemType = "QUESTION";
            newItem.Isvisible = "false";
            newItem.ListChildItem = new List<Item>();
            return newItem;
        }
        public Boolean renameNodeForRemediationTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean renameCourseForRemediationTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Organization item = findOrganizationByName(selectItemName);
                item.Title = input.Text;
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForRemediationTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                if (item.ItemType == "MODULE")
                {
                    Organization org = findParentOfModule(item);
                    org.ListItem.Remove(item);
                    List<Item> listItem = imsmanifest.ManifestOrganizations.ListOrganization[0].ListItem;
                    //remove first exam part of module
                    Item fistExamItem = getFirstExamItem();
                    for (int i = 0; i < fistExamItem.ListChildItem.Count; i++)
                    {
                        if (fistExamItem.ListChildItem[i].Identifier == "fp" + item.Identifier)
                        {
                            fistExamItem.ListChildItem.Remove(fistExamItem.ListChildItem[i]);
                            break;
                        }
                        else
                            continue;
                    }
                    //remove remediate of module

                    for (int i = getLastestModule(); i < listItem.Count; i++)
                    {
                        if (listItem[i].Identifier == "rm" + item.Identifier)
                        {
                            listItem.Remove(listItem[i]);
                            break;
                        }
                        else
                            continue;
                    }
                    //remove final exam part of module
                    Item finalExamItem = getExamItem();
                    for (int i = 0; i < finalExamItem.ListChildItem.Count; i++)
                    {
                        if (finalExamItem.ListChildItem[i].Identifier == "fe" + item.Identifier)
                        {
                            finalExamItem.ListChildItem.Remove(finalExamItem.ListChildItem[i]);
                            break;
                        }
                        else
                            continue;
                    }
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    parent.ListChildItem.Remove(item);
                }
                handleDeleteResource(item);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        #endregion

        public Boolean SwapSelectedItemToNext(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Organization org = findParentOfModule(item);
                if (org != null)
                {
                    if (swapInfo != null && swapInfo.CurItemIndex != null && swapInfo.CurItemIndex >= 0 && swapInfo.NextItemIndex != null && swapInfo.NextItemIndex >= 0)
                    {
                        SwapItemToNext(org.ListItem, swapInfo);
                        result = true;
                    }
                    else
                        result = false;

                }
                else
                {
                    Item parent = findParentOfItem(item);
                    if (parent != null)
                    {
                        if (swapInfo != null && swapInfo.CurItemIndex != null && swapInfo.CurItemIndex >= 0 && swapInfo.NextItemIndex != null && swapInfo.NextItemIndex >= 0)
                        {
                            SwapItemToNext(parent.ListChildItem, swapInfo);
                            result = true;
                        }
                        else
                            result = false;
                    }
                }
               
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public void SwapItemToNext(List<Item> list, SwapInfo swapInfo)
        {
            Item tmp = list[swapInfo.CurItemIndex];
            list[swapInfo.CurItemIndex] = list[swapInfo.NextItemIndex];
            list[swapInfo.NextItemIndex] = tmp;
        }
        public void SwapItemToPrevious(List<Item> list, SwapInfo swapInfo)
        {
            Item tmp = list[swapInfo.PreItemIndex];
            list[swapInfo.PreItemIndex] = list[swapInfo.CurItemIndex];
            list[swapInfo.CurItemIndex] = tmp;
        }
        
        public Boolean SwapSelectItemToPrevious(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                string selectItemName = getNameOfTreeViewItem(selectedItem);
                Item item = findItemByName(selectItemName);
                Organization org = findParentOfModule(item);
                if (org != null)
                {
                    if (swapInfo != null  && swapInfo.CurItemIndex >= 0  && swapInfo.PreItemIndex >= 0)
                    {
                        SwapItemToPrevious(org.ListItem, swapInfo);
                        result = true;
                    }
                    else
                        result = false;
                }
                else
                {
                    Item parent = findParentOfItem(item);
                    if (parent != null)
                    {
                        if (swapInfo != null  && swapInfo.CurItemIndex >= 0  && swapInfo.PreItemIndex >= 0)
                        {
                            SwapItemToPrevious(parent.ListChildItem, swapInfo);
                             result = true;
                        }else
                            result = false;
                    }
                }
               
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
    }
}
